﻿/**
* 命名空间: ICS_TricolorLamp
* 功 能：温度计
* 类 名： Thermometer
*
* Ver 变更日期 负责人 变更内容
* ───────────────────────────────────
* V0.01 2021/9/15 11:25:11 小柚子爱学习 初版
*
* Copyright (c) 2021 Lir Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为个人机密信息，未经本人书面同意禁止向第三方披露．　│
*│　版权所有：Vip_Future 　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace ICS_TricolorLamp
{
    /// <summary>
    /// Thermometer.xaml 的交互逻辑
    /// </summary>
    public partial class Thermometer : UserControl
    {
        public Thermometer()
        {
            InitializeComponent();
            this.SizeChanged += Thermometer_SizeChanged;
        }

        private void Thermometer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Refresh();
        }

        //最小值
        public int Minimum {
            get { return (int)GetValue(MinimumProperty); }

            set { SetValue(MinimumProperty, value); } 
        }
        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.Register("Minimum", typeof(int), typeof(Thermometer), new PropertyMetadata(-10,new PropertyChangedCallback(OnRangeChanged)));
        //最大值
        public int Maximum
        {
            get { return (int)GetValue(MaximumProperty); }

            set { SetValue(MaximumProperty, value); }
        }
        public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(int), typeof(Thermometer), new PropertyMetadata(100, new PropertyChangedCallback(OnRangeChanged)));
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(double), typeof(Thermometer), new PropertyMetadata(0.0, new PropertyChangedCallback(OnRangeChanged)));

        //执行对象回掉方法
        private static void OnRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as Thermometer).Refresh();
        //画刻度：需要两个属性
        private void Refresh() {
            //两种方式：尺寸变化 、区间变化
            //h获取高度 判断页面元素是否加载
           var h= this.mainCanvas.ActualHeight;
            if (h == 0) return;
            //宽度
            double w = 75;
          

            //在这个区间内划分多少间隔
            double stepCount = Maximum - Minimum;
            //每个间隔的距离
            double step = h / (Maximum - Minimum);
            //清空先前画布
            this.mainCanvas.Children.Clear();
            //循环添加刻度线
            for (int i = 0; i <=stepCount; i++)
            {
                Line line = new Line();
                
                line.Y1 = i * step;
                line.Y2 = i * step;
                line.Stroke = Brushes.Black;
                line.StrokeThickness = 1;
                this.mainCanvas.Children.Add(line);

                if (i % 10 == 0)
                {
                    line.X1 = 15;
                    line.X2 = w - 15;
                    //添加文字
                    TextBlock text = new TextBlock
                    {
                        Text = (Maximum - i).ToString(),
                        Width = 20,
                        TextAlignment = TextAlignment.Center,
                        FontSize = 9,
                        Margin = new Thickness(0, -5, -4, 0)
                    };
                    Canvas.SetLeft(text, w - 15);
                    Canvas.SetTop(text, i * step);
                    this.mainCanvas.Children.Add(text);
                    //添加文字
                    TextBlock text1 = new TextBlock
                    {
                        Text = (Maximum - i).ToString(),
                        Width = 20,
                        TextAlignment = TextAlignment.Center,
                        FontSize = 9,
                        Margin = new Thickness(-4, -5, 0, 0)
                    };
                    Canvas.SetLeft(text1, 0);
                    Canvas.SetTop(text1, (i*step));
                    this.mainCanvas.Children.Add(text1);
                }
                else if (i % 5 == 0)
                {
                    line.X1 = 20;
                    line.X2 = w - 20;
                }
                else {
                    line.X1 = 25;
                    line.X2 = w - 25;
                }

            }
            //值的变化
            var value = this.Value;
            if (this.Value<this.Minimum)
            {
                value = this.Minimum;
            }
            if (this.Value> this.Maximum)
            {
                value = this.Maximum;
            }
            //温度值与Border的高度转换
            var newValue = this.Value - this.Minimum;
            newValue *=step;
            newValue += 6;
            DoubleAnimation doubleAnimation = new DoubleAnimation(newValue, TimeSpan.FromMilliseconds(500));
            this.borValue.BeginAnimation(HeightProperty, doubleAnimation);

        }
      
    }
}
